//index.js
//获取应用实例
const app = getApp()

var interval;
var varName;
var ctx = wx.createCanvasContext('canvasArcCir');

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    src:'',
    camera:'',
    code:"http://localhost:3805/img/201810252158.png",
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    this.downloadImg();
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  onReady:function(){
    if (wx.createCameraContext()) {
      this.cameraContext = wx.createCameraContext('myCamera')
    } else {
      // 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示  
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
    //创建并返回绘图上下文context对象。
    var cxt_arc = wx.createCanvasContext('canvasCircle');
    cxt_arc.setLineWidth(16);
    cxt_arc.setStrokeStyle('#eaeaea');
    cxt_arc.setLineCap('round');
    cxt_arc.beginPath();
    cxt_arc.arc(100, 100, 50, 0, 2 * Math.PI, false);
    cxt_arc.stroke();


    cxt_arc.draw();   
    
  },
  showVideo:function(e){
    var that=this;
    wx.chooseVideo({
      sourceType: ['camera'],
      maxDuration: 10,
      success: function (res) {
        that.setData({
          src: res.tempFilePath
        })
      }
    })
  },
  takePhoto() {
    this.cameraContext.startRecord({
      success: function (res) {
        console.log('成功！')
        console.log(res)
      },
      fail: function (res) {
        console.log('失败！')
        console.log(res)
      },
      complete: function (res) {
        console.log('complete！')
        console.log(res)
      },
      timeoutCallback: function (res) {
        console.log('超时')
        console.log(res)
      }
      }
    )
  },
  drawCircle: function () {
    let that=this;
    clearInterval(varName);
    function drawArc(s, e) {
      var x = 100, y = 100, radius = 50;
      ctx.setLineWidth(16);
      ctx.setStrokeStyle('#09bb07');
      ctx.setLineCap('round');
      ctx.beginPath();
      ctx.arc(x, y, radius, s, e, false); 
      ctx.drawImage(that.data.code, 0, 0, 430, 430);     
      ctx.stroke()
      ctx.draw()
    }
    var step = 1, startAngle = 1.5 * Math.PI, endAngle = 0;
    var animation_interval = 1, n = 300;
    var animation = function () {
      if (step <= n) {
        endAngle = step * 2 * Math.PI / n + 1.5 * Math.PI;
        drawArc(startAngle, endAngle);
        step++;
      } else {
        clearInterval(varName);
      }
    };
    varName = setInterval(animation, animation_interval);
  },
  downloadImg:function(e){
        // 下载二维码
    

    wx.getImageInfo({
      src: 'http://localhost:3805/img/201810252158.png',
      success: function (res) {
        _this.setData({
          imgwidth: res.width,
          imgheight: res.height,
          code:res.path
        })
      }
    })
  }
})